package com.pengtu.web.business;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.application.FlowApplication;
import com.pengtu.entity.business.check.FlowCompletion;
import com.pengtu.entity.business.labs.LabsAuthorization;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.quality.FlowProject;
import com.pengtu.model.ProjectModel;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.business.ApplicationManager;
import com.pengtu.service.business.CompletionManager;
import com.pengtu.service.business.LabsFilingManager;
import com.pengtu.service.history.HistoryProjectManager;
import com.pengtu.service.quality.FprojectManager;
import com.pengtu.utils.web.ControlHelpUtil;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({
		@Result(name = BaseActionSupport.RELOAD, location = "${ctx}/obusiness/application!input.action?id=${qualityApplicationId}", type = "redirect"),
		@Result(name = Constants.INFO, location = "${ctx}/business/application.action", type = "redirect"),
		@Result(name = BaseActionSupport.VIEW, location = "${ctx}/quality/plan!input.action?id=${flowPlanId}", type = "redirect"),
		@Result(name = BaseActionSupport.NONE, location = "${ctx}/quality/plan!list.action", type = "redirect"),
		@Result(name = Constants.ASSIGN, location = "${ctx}/obusiness/labs!input.action?id=${labsId}", type = "redirect"),
		@Result(name = Constants.PAGE, location = "${ctx}/business/labs!find.action", type = "redirect"),
		@Result(name = BaseActionSupport.LOGIN, location = "${ctx}/obusiness/detection.action", type = "redirect"),
		@Result(name = Constants.COMPLY, location = "${ctx}/business/detection.action", type = "redirect"),
		@Result(name = "accept1", location = "${ctx}/obusiness/acceptance.action", type = "redirect"),
		@Result(name = "accept2", location = "${ctx}/business/acceptance.action", type = "redirect"),
		@Result(name = "comple1", location = "${ctx}/obusiness/completion!input.action?id=${flowCompletionId}", type = "redirect"),
		@Result(name = "comple2", location = "${ctx}/business/completion.action", type = "redirect"),
		@Result(name = "input1", location = "${ctx}/business/fproject!input.action", type = "redirect") })
public class FprojectAction extends BaseActionSupport<FlowProject> {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private FprojectManager fprojectManager;
	private HistoryProjectManager historyProjectManager;
	private LabsFilingManager labsFilingManager;
	private CompletionManager completionManager;
	private ApplicationManager applicationManager;

	private String id;
	private FlowProject entity;
	private String qualityApplicationId;
	private String flowPlanId;
	private String labsId;
	private String flowDetectionId;
	private String flowAcceptanceId;
	private String flowCompletionId;

	@Autowired
	public void setFprojectManager(FprojectManager fprojectManager) {
		this.fprojectManager = fprojectManager;
	}

	@Autowired
	public void setHistoryProjectManager(
			HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	@Autowired
	public void setLabsFilingManager(LabsFilingManager labsFilingManager) {
		this.labsFilingManager = labsFilingManager;
	}

	@Autowired
	public void setApplicationManager(ApplicationManager applicationManager) {
		this.applicationManager = applicationManager;
	}

	@Autowired
	public void setCompletionManager(CompletionManager completionManager) {
		this.completionManager = completionManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLabsId() {
		return labsId;
	}

	public void setLabsId(String labsId) {
		this.labsId = labsId;
	}

	public FlowProject getEntity() {
		return entity;
	}

	public void setEntity(FlowProject entity) {
		this.entity = entity;
	}

	public FlowProject getModel() {
		return entity;
	}

	public String list() throws Exception {
		return SUCCESS;
	}

	public String getQualityApplicationId() {
		return qualityApplicationId;
	}

	public void setQualityApplicationId(String qualityApplicationId) {
		this.qualityApplicationId = qualityApplicationId;
	}

	public String getFlowPlanId() {
		return flowPlanId;
	}

	public void setFlowPlanId(String flowPlanId) {
		this.flowPlanId = flowPlanId;
	}

	public String getFlowDetectionId() {
		return flowDetectionId;
	}

	public void setFlowDetectionId(String flowDetectionId) {
		this.flowDetectionId = flowDetectionId;
	}

	public String getFlowAcceptanceId() {
		return flowAcceptanceId;
	}

	public void setFlowAcceptanceId(String flowAcceptanceId) {
		this.flowAcceptanceId = flowAcceptanceId;
	}

	public String getFlowCompletionId() {
		return flowCompletionId;
	}

	public void setFlowCompletionId(String flowCompletionId) {
		this.flowCompletionId = flowCompletionId;
	}

	public String input() throws Exception {
		return INPUT;
	}

	public String save() throws Exception {
		String resourceName = null;
		if (entity.getKey().equals("apply")) {
			resourceName = "/flow/apply.zip";
		} else if (entity.getKey().equals("program")) {
			resourceName = "/flow/program.zip";
		} else if (entity.getKey().equals("machine")) {
			resourceName = "/flow/machine";
		} else if (entity.getKey().equals("comple")) {
			resourceName = "/flow/comple.zip";
		} else if (entity.getKey().equals("finish")) {
			resourceName = "/flow/finish.zip";
		} else if (entity.getKey().equals("labs")) {
			resourceName = "/flow/labs.zip";
		} else if (entity.getKey().equals("detection")) {
			resourceName = "/flow/detection.zip";
		} else if (entity.getKey().equals("accept")) {
			resourceName = "/flow/accept.zip";
		} else if (entity.getKey().equals("convey")) {
			resourceName = "/flow/convey.zip";
		} else if (entity.getKey().equals("received")) {
			resourceName = "/flow/received.zip";
		} else if (StringUtils.equals(entity.getKey(), "check")) {
			resourceName = "/flow/check.zip";
		} else if (StringUtils.equals(entity.getKey(), "plan")) {
			resourceName = "/flow/plan.zip";
		} else if (StringUtils.equals(entity.getKey(), "oconvey")) {
			resourceName = "/flow/oconvey.zip";
		} else if (StringUtils.equals(entity.getKey(), "mdetect")) {
			resourceName = "/flow/mdetect.zip";
		} else if (StringUtils.equals(entity.getKey(), "dmarine")) {
			resourceName = "/flow/dmarine.zip";
		} else if (StringUtils.equals(entity.getKey(), "cmarine")) {
			resourceName = "/flow/cmarine.zip";
		} else if (StringUtils.equals(entity.getKey(), "recheck")) {
			resourceName = "/flow/recheck.zip";
		} else if (StringUtils.equals(entity.getKey(), "estimate")) {
			resourceName = "/flow/estimate.zip";
		} else if (StringUtils.equals(entity.getKey(), "budget")) {
			resourceName = "/flow/budget.zip";
		} else if (StringUtils.equals(entity.getKey(), "limited")) {
			resourceName = "/flow/limited.zip";
		} else if (StringUtils.equals(entity.getKey(), "drawing")) {
			resourceName = "/flow/drawing.zip";
		} else if (StringUtils.equals(entity.getKey(), "completed")) {
			resourceName = "/flow/completed.zip";
		} else if (StringUtils.equals(entity.getKey(), "modify")) {
			resourceName = "/flow/modify.zip";
		} else if (StringUtils.equals(entity.getKey(), "leave")) {
			resourceName = "/flow/leave.zip";
		} else if (StringUtils.equals(entity.getKey(), "document")) {
			resourceName = "/flow/document.zip";
		} else if (StringUtils.equals(entity.getKey(), "entrust")) {
			resourceName = "/flow/entrust.zip";
		} else if (StringUtils.equals(entity.getKey(), "order")) {
			resourceName = "/flow/order.zip";
		}
		fprojectManager.deploy(resourceName, entity);
		return "input1";
	}

	public String delete() throws Exception {
		return null;
	}

	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = fprojectManager.get(id);
		} else {
			entity = new FlowProject();
		}
	}

	public String apply() throws Exception {
		String result = null;
		ProjectModel projectModel = new ProjectModel();
		String projId = Struts2Utils.getParameter("projId");
		String status = fprojectManager.getById(projId);
		ProjectInfo projectInfo = fprojectManager.find(projId);
		User user = SpringSecurityUtils.getCurrentUser();
		String deploymentId = fprojectManager.getByKey("apply")
				.getDeploymentId();

		FlowApplication flowApplication = applicationManager
				.getFlowApplication(projectInfo);
		String taskId = historyProjectManager.getTaskId(projectInfo, "10001");
		if (flowApplication == null) {
			qualityApplicationId = null;
		} else {
			qualityApplicationId = flowApplication.getId();
		}
		Struts2Utils.getSession().setAttribute("organization",
				user.getOrganization());
		if (status.equals("10001")) {
			result = RELOAD;
		} else {
			result = Constants.INFO;
		}
		projectModel.setDeploymentId(deploymentId);
		projectModel.setProjId(projectInfo.getId());
		projectModel.setProjName(projectInfo.getProjName());
		if (StringUtils.isNotEmpty(taskId)) {
			projectModel.setTaskId(taskId);
			Struts2Utils.getRequest().setAttribute("taskId", taskId);
		}
		ControlHelpUtil.setProjectToSession(projectModel);
		ControlHelpUtil.setObjectToSession("projInfo", projectInfo);
		ControlHelpUtil.setObjectToSession("deploymentId", deploymentId);
		return result;
	}

	public String detn() throws Exception {
		String result = null;
		return result;
	}

	public String view() throws Exception {
		ProjectModel projectModel = new ProjectModel();
		String projId = Struts2Utils.getParameter("projId");
		String status = fprojectManager.getById(projId);
		ProjectInfo projectInfo = fprojectManager.find(projId);
		User user = SpringSecurityUtils.getCurrentUser();
		String result = "";
		String deploymentId = "";
		String taskId = "";
		switch (Integer.parseInt(status.substring(0, 1))) {
		case 1:
			deploymentId = fprojectManager.getByKey("apply").getDeploymentId();

			FlowApplication flowApplication = applicationManager
					.getFlowApplication(projectInfo);
			taskId = historyProjectManager.getTaskId(projectInfo, "10001");
			if (flowApplication == null) {
				qualityApplicationId = null;
			} else {
				qualityApplicationId = flowApplication.getId();
			}
			Struts2Utils.getSession().setAttribute("organization",
					user.getOrganization());
			if (status.equals("10001")) {
				result = RELOAD;
			} else {
				result = Constants.INFO;
			}
			break;
		case 4:
			switch (Integer.parseInt(status.substring(2, 3))) {
			case 0:
				deploymentId = fprojectManager.getByKey("detection")
						.getDeploymentId();
				taskId = historyProjectManager.getTaskId(projectInfo, "40001");
				if (status.equals("40001")) {
					result = LOGIN;
				} else {
					result = Constants.COMPLY;
				}
				break;
			case 1:
				taskId = historyProjectManager.getTaskId(projectInfo, "40101");
				deploymentId = fprojectManager.getByKey("accept")
						.getDeploymentId();
				if (status.equals("40101")) {
					result = "accept1";
				} else {
					result = "accept2";
				}
				break;
			}
			break;
		case 5:
			taskId = historyProjectManager.getTaskId(projectInfo, "50001");
			deploymentId = fprojectManager.getByKey("comple").getDeploymentId();
			FlowCompletion flowCompletion = completionManager
					.getFlowCompletion(projectInfo);
			if (flowCompletion != null) {
				flowCompletionId = flowCompletion.getId();
				ControlHelpUtil.setObjectToSession("fId", flowCompletionId);
			} else {
				flowCompletionId = null;
			}
			if ("50001".equals(status)) {
				result = "comple1";
			} else {
				result = "comple2";
			}
			break;
		default:
			break;
		}
		projectModel.setDeploymentId(deploymentId);
		projectModel.setProjId(projectInfo.getId());
		projectModel.setProjName(projectInfo.getProjName());
		if (StringUtils.isNotEmpty(taskId)) {
			projectModel.setTaskId(taskId);
			Struts2Utils.getRequest().setAttribute("taskId", taskId);
		}
		ControlHelpUtil.setProjectToSession(projectModel);
		ControlHelpUtil.setObjectToSession("projInfo", projectInfo);
		ControlHelpUtil.setObjectToSession("deploymentId", deploymentId);
		return result;
	}

	public String info() throws Exception {
		String projId = Struts2Utils.getParameter("projId");
		ProjectInfo projectInfo = fprojectManager.find(projId);
		ProjectModel projectModel = new ProjectModel();
		String deploymentId = fprojectManager.getByKey("labs")
				.getDeploymentId();
		List<LabsAuthorization> flowLabsList = labsFilingManager
				.getLabsAuthorization(projectInfo);
		projectModel.setProjId(projId);
		projectModel.setProjName(projectInfo.getProjName());

		ControlHelpUtil.setObjectToRequest("flowLabsList", flowLabsList);

		if (StringUtils.isNotEmpty(deploymentId)) {
			projectModel.setDeploymentId(deploymentId);
		}
		ControlHelpUtil.setProjectToSession(projectModel);
		return SUCCESS;
	}

}
